package com.example.run_game;

import java.util.Date;

import android.util.Log;
import android.widget.Toast;

//This class stores the current score of the player and handles modifications to the score from bonuses and such
//Also at the end of a run save all the progress made

public class ScoreManager
{
  ///same as xp gained over this race
  public int score =0 ;
  private int scoreUpdate =0;
  private long bonusStarted;
  private String timerBonus = "";
  
  Player player;
  Run rungiu;
  SoundManager sound;
  
  //pointmodifiers last for 5 minutes
  //speed challenges last for 3 minutes
  private final int pointModifierDuration = 300;
  private final int speedChallengeDuration = 180;
  private double speedChallengeInitialSpeed;

  ScoreManager(Player playerIn, Run rungiu, SoundManager soundsIn)
  {
	player = playerIn;
	this.rungiu = rungiu;
	sound = soundsIn;
  }
  
  public void applyBonus(double distanceMoved, String bonusInEffect, double avSpeedOverRun)
  {
	  //apply bonus to scoreUpdate
	  	//check timers and reset vars
	  Date date = new Date(); 
	  String[] filenames = {"","","","",""};
	  if(bonusInEffect.equals("extra800"))	  
	  {
		Toast.makeText(rungiu.getApplicationContext(), "Bonus 800xp gained",Toast.LENGTH_SHORT).show();
		filenames[0] = "bonus_unlocked.mp3";
		filenames[1] = "800xp.mp3";
		sound.playSound(filenames);
		scoreUpdate += 800;	  
	  }
	  if(bonusInEffect.equals("extra500"))	  	
	  {
		  Toast.makeText(rungiu.getApplicationContext(), "Bonus 500xp gained",Toast.LENGTH_SHORT).show();
		  filenames[0] = "bonus_unlocked.mp3";
		  filenames[1] = "500xp.mp3";
		  sound.playSound(filenames);
		  scoreUpdate += 500;	  
	  }
	  if(bonusInEffect.equals("2xPoints"))
	  {		  
		  Toast.makeText(rungiu.getApplicationContext(), "Triple xp for 5 minutes",Toast.LENGTH_SHORT).show();
		  filenames[0] = "bonus_unlocked.mp3";
		  filenames[1] = "triple_xp_unlock.mp3";
		  sound.playSound(filenames);
		  timerBonus = "2xPoints";	      
	      bonusStarted = date.getTime();
	  }	  
	  if(bonusInEffect.equals("1.5xpoints"))
	  {		  
		  Toast.makeText(rungiu.getApplicationContext(), "Double xp for 5 minutes",Toast.LENGTH_SHORT).show();
		  filenames[0] = "bonus_unlocked.mp3";
		  filenames[1] = "double_xp_unlock.mp3";
		  sound.playSound(filenames);
		  timerBonus = "1.5xpoints";	      
	      bonusStarted = date.getTime();
	  }	    
	  if(bonusInEffect.equals("runFast1000"))
	  {		
		  Toast.makeText(rungiu.getApplicationContext(), "Raise your speed for 1000xp",Toast.LENGTH_SHORT).show();
		  filenames[0] = "run_faster.mp3";
		  sound.playSound(filenames);
		  timerBonus = "runFast1000";	      
	      bonusStarted = date.getTime();
	      speedChallengeInitialSpeed = avSpeedOverRun;
	  }	  
	  if(bonusInEffect.equals("runFast800"))
	  {		
		  Toast.makeText(rungiu.getApplicationContext(), "Raise your speed for 800xp",Toast.LENGTH_SHORT).show();
		  filenames[0] = "run_faster.mp3";
		  sound.playSound(filenames);
		  timerBonus = "runFast800";	      
	      bonusStarted = date.getTime();
	      speedChallengeInitialSpeed = avSpeedOverRun;
	  }	 
	  
	  
	  //Check if any timed bonuses are running
	  long currentTime = date.getTime();
	  long timeSinceBonusStart = currentTime - bonusStarted;
	  if(	(timerBonus.equals("2xPoints") || 
			 timerBonus.equals("1.5xpoints")) &&
			 timeSinceBonusStart > pointModifierDuration)		  
		  timerBonus = "";
	   
	  if(timerBonus.equals("runFast800") && timeSinceBonusStart > speedChallengeDuration)	
	  {
		  timerBonus = "";
		  if( avSpeedOverRun > speedChallengeInitialSpeed*1.1 )
		  {
			  scoreUpdate += 800;
			  Toast.makeText(rungiu.getApplicationContext(), "Speed increased, 800xp gained!",Toast.LENGTH_SHORT).show();
			  filenames[0] = "speed_increased.mp3";
			  filenames[1] = "800xp.mp3";
			  sound.playSound(filenames);
		  }
	  }	  
	  if(timerBonus.equals("runFast1000") && timeSinceBonusStart > speedChallengeDuration)	
	  {
		  timerBonus = "";
		  if( avSpeedOverRun > speedChallengeInitialSpeed*1.1 )
		  {
			  scoreUpdate += 1000;
			  Toast.makeText(rungiu.getApplicationContext(), "Speed increased, 1000xp gained!",Toast.LENGTH_SHORT).show();
			  filenames[0] = "speed_increased.mp3";
			  filenames[1] = "1000xp.mp3";
		  }
	  }			      
	  if(timerBonus.equals("2xPoints"))	  	
		  scoreUpdate = scoreUpdate * 2;	  
	  if(timerBonus.equals("1.5xpoints"))	  
		  scoreUpdate = (int)(scoreUpdate * 1.5);
  }
  
  public void updateScore(double distanceMoved, Choice currChoice, double avSpeedOverRun, int playerbearing, int windDirection)
  {
	  scoreUpdate = (int)distanceMoved;
	  String bonusInEffect = "";
  	  if(currChoice.bonusThresholdReached())    	
  		bonusInEffect = currChoice.getGoalChoicePair().getBonus(currChoice);
	  applyBonus(distanceMoved, bonusInEffect, avSpeedOverRun);
	  
	  if(bonusInEffect != "")
		  Log.d("GAME:", "Bonus: " + bonusInEffect);
	  
	  if(playerbearing != -1 && windDirection != -1)
	  {
		  int windDirectionUpper =  (windDirection + 23) % 360;
		  int windDirectionLower = (windDirection - 23) % 360;
		  if(windDirectionLower < playerbearing && windDirectionUpper > playerbearing)
		  {
			  scoreUpdate = (int)(1.3 * scoreUpdate);
			  Toast.makeText(rungiu.getApplicationContext(), "Running into the wind, 30% bonus xp!",Toast.LENGTH_SHORT).show();
		  }
	  }
	  
	  player.awardXP(scoreUpdate);
	  
	  //Log.d("Score: ", "score before update: "+ score);
	  //Log.d("Score: ", "scoreUpdate: "+ scoreUpdate);
	  score += scoreUpdate;
	  
  }  
  
  public void subObjectiveXPBonus(int xp)
  {
	  score += xp;	  
	  Log.d("Score: ", "score after subojective update: "+ score);
  }  
}
